﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Ozone.Views.Widget
{
    /// <summary>
    /// UCSwitchWork.xaml 的交互逻辑
    /// </summary>
    public partial class UCSwitchWork : UserControl
    {
        public UCSwitchWork()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 设置工作状态和显示名称
        /// </summary>
        /// <param name="bStatus">true:工作中, false:等待中</param>
        /// <param name="DevName">设备名称(4个汉字)</param>
        public void SetWorkState(bool bStatus, string DevName)
        {
            // 切换图片显示状态
            if (bStatus)
            {
                // 工作中状态
                imgSwitchworking.Visibility = Visibility.Visible;
                imgSwitchworkawait.Visibility = Visibility.Collapsed;
            }
            else
            {
                // 等待中状态
                imgSwitchworkawait.Visibility = Visibility.Visible;
                imgSwitchworking.Visibility = Visibility.Collapsed;
            }

            // 设置设备名称显示
            if (!string.IsNullOrEmpty(DevName))
            {
                UCWorkName.Content = DevName;
            }
        }

        /// <summary>
        /// 依赖属性：工作状态
        /// </summary>
        public static readonly DependencyProperty WorkStatusProperty =
            DependencyProperty.Register("WorkStatus", typeof(bool), typeof(UCSwitchWork),
                new PropertyMetadata(true, OnWorkStatusChanged));

        public bool WorkStatus
        {
            get { return (bool)GetValue(WorkStatusProperty); }
            set { SetValue(WorkStatusProperty, value); }
        }

        /// <summary>
        /// 依赖属性：设备名称
        /// </summary>
        public static readonly DependencyProperty DeviceNameProperty =
            DependencyProperty.Register("DeviceName", typeof(string), typeof(UCSwitchWork),
                new PropertyMetadata("液体进样", OnDeviceNameChanged));

        public string DeviceName
        {
            get { return (string)GetValue(DeviceNameProperty); }
            set { SetValue(DeviceNameProperty, value); }
        }

        private static void OnWorkStatusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as UCSwitchWork;
            if (control != null && e.NewValue is bool)
            {
                control.SetWorkState((bool)e.NewValue, control.DeviceName);
            }
        }

        private static void OnDeviceNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as UCSwitchWork;
            if (control != null && e.NewValue is string)
            {
                control.SetWorkState(control.WorkStatus, (string)e.NewValue);
            }
        }
    }
}